package com.situation.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situation.mapper.BizProductMapper;
import com.situation.mapper.OutStockInfoMapper;
import com.situation.mapper.OutStockMapper;
import com.situation.pojo.DO.BizConsumer;
import com.situation.pojo.DO.BizOutStock;
import com.situation.pojo.DO.BizOutStockInfo;
import com.situation.pojo.DO.BizProduct;
import com.situation.pojo.VO.OutStockVO;
import com.situation.service.OutStockService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

@Service
public class OutStockServiceImpl implements OutStockService {

    @Autowired
    private OutStockMapper outStockMapper;

    @Autowired
    private OutStockInfoMapper outStockInfoMapper;


    @Autowired
    private BizProductMapper productMapper;


    /**
     * @param pageNum  第几页
     * @param pageSize 每页显示
     * @param outStock 实体类
     * @return
     */
    @Override
    public PageInfo<OutStockVO> queryAll(int pageNum, int pageSize, BizOutStock outStock) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(outStockMapper.queryAll(outStock));
    }

    /**
     * 查询明细
     *
     * @param pageNum  第几页
     * @param pageSize 显示条数
     * @param outNum   发放单号
     * @return
     */
    @Override
    public PageInfo<OutStockVO> queryInfo(int pageNum, int pageSize, String outNum) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(outStockMapper.queryInfo(outNum));
    }

    /**
     * 发放明细收货人
     *
     * @param outNum
     * @return
     */
    @Override
    public BizConsumer queryCon(String outNum) {
        return outStockMapper.queryCon(outNum);
    }

    /**
     * 删除  真删
     *
     * @param outNum 主键
     * @return boolean
     */
    @Override
    @Transactional
    public Boolean deleteById(String outNum) {
        Long aLong = outStockInfoMapper.countOutNum(outNum);
        int i = outStockMapper.deleteById(outNum);
        int i1 = outStockInfoMapper.deleteById(outNum);
        return aLong == i1 && i == 1;
    }

    /**
     * 修改状态
     *
     * @param outNum
     * @param status
     * @return
     */
    @Override
    @Transactional
    public Boolean updateStatus(String outNum, int status) throws IllegalAccessException {

        List<BizOutStockInfo> infoList = outStockMapper.findPnum(outNum);
        // 已通过,修改库存

        for (BizOutStockInfo info : infoList) {
            // 申请出库的数量
            Integer number = info.getProductNumber();
            // 库存总数
            Long stock = outStockMapper.findStock(info.getPNum());
            if (number > stock) {
                throw new IllegalAccessException("物资库存不够");
            }
            outStockMapper.updateStock(info.getPNum(), stock - number);
        }

        return outStockMapper.updateStatus(outNum, status) > 0;
    }

    /**
     * 添加
     *
     * @param outStockVO
     * @return
     */
    @Override
    @Transactional
    public void insert(OutStockVO outStockVO) throws IllegalAccessException {

        //随机生成发放单号
        String OUT_STOCK_NUM = UUID.randomUUID().toString();
        int itemNumber = 0;//记录该单的总数
        //获取商品的明细
        List<BizOutStockInfo> products = outStockVO.getProducts();
        if (!CollectionUtils.isEmpty(products)) {
            for (BizOutStockInfo product : products) {

                //发放数量
                int productNumber = product.getProductNumber();
                //物资编号,不是主键
                String pNum = product.getPNum();
                BizProduct dbProduct = productMapper.findProBypNum(pNum);
                if (dbProduct == null) {
                    throw new IllegalAccessException("产品不存在");
                } else if (productNumber <= 0) {
                    throw new IllegalAccessException("发放数量不合法,无法入库");
                } else {
                    //校验库存

                    Long stockBypNum = productMapper.getStockBypNum(pNum);

                    if (productNumber > stockBypNum) {
                        throw new IllegalAccessException("库存不足,库存剩余:");
                    }

                    //入库单明细
                    BizOutStockInfo outStockInfo = new BizOutStockInfo();
                    outStockInfo.setCreateTime(LocalDateTime.now());
                    outStockInfo.setModifiedTime(LocalDateTime.now());
                    outStockInfo.setProductNumber(productNumber);
                    outStockInfo.setPNum(pNum);
                    outStockInfo.setOutNum(OUT_STOCK_NUM);
                    outStockInfoMapper.insert(outStockInfo);
                    itemNumber += productNumber;
                }
            }
            BizOutStock outStock = new BizOutStock();

            outStock.setCreateTime(LocalDateTime.now());
            outStock.setProductNumber(itemNumber);
            String username = (String) SecurityUtils.getSubject().getPrincipal();
            outStock.setOperator(username);
            outStock.setType(outStockVO.getType());
            outStock.setConsumerId(outStockVO.getConsumerId());
            outStock.setPriority(outStockVO.getPriority());
            outStock.setRemark(outStockVO.getRemark());
            //生成入库单
            outStock.setOutNum(OUT_STOCK_NUM);
            //设置为待审核
            outStock.setStatus(2);
            outStockMapper.insert(outStock);
        } else {
            throw new IllegalAccessException("物资不能为空");
        }


    }


}
